package de.fhd.pms;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.If;

public class ServPmsBgTask extends HttpServlet implements Runnable {
	
	private String stringUrl;
	Thread t = null;
	boolean running = false;
    Image webcamImage = null;
    HttpServletRequest Request = null;
    HttpServletResponse Response = null;
    boolean state = false;
    
	public void init() {
		URL	urlWebcam;
		t = new Thread(this);
		t.start();
		System.out.println("thread start");
		//stringUrl = getInitParameter("URL");
		
		//urlWebcam = new URL(stringUrl);
		//webcamImage = getImage(urlWebcam);
	}
 
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	  throws ServletException, IOException
	 {
		
		Request = request;	
		Response = response;
		
		    PrintWriter out = response.getWriter();
		     out.println("<html>");
				//Request = request;
				//Response = response;
				//doPost(request,response);
		     out.println("true");
		     out.println("</html>");
		     out.flush();
//		 while(state = true) { 
//			 //response.setContentType( "text/html" );
//		     //PrintWriter out = response.getWriter();
//		     out.println("<html>");
//				//Request = request;
//				//Response = response;
//				//doPost(request,response);
//		     out.println("3");
//		     out.println("</html>");
//		     System.out.println("doGet " + state);
//	         state = false; 
//	   }
	
	 doPost(request,response);
	 }
             
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//response.setContentType("text/html");
		//PrintWriter out = response.getWriter();
		//out.println("ServPmsBgTask=" + running);
		//public void run() {};
		
		  //Set the mime type of the image  
          response.setContentType("image/jpeg");  
          	try  
          		{  
          		// Create an image 200 x 200  
          		BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);  
          		//Draw an oval  
          		Graphics g = bufferedImage.getGraphics();  
          		g.setColor(Color.blue);  
          		g.fillOval(0, 0, 199,199);  
                // Free graphic resources  
          		g.dispose();  
          		
          		g.drawImage(bufferedImage, 0,0, (ImageObserver) this);
          		//Write the image as a jpg  
          		ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); 
          		}  
          		catch (IOException ioe)  
          		{  
     }
}

	
	public void run() {
		long timeMillis = 1000;
		long counter = 0;
		while (true) {
			counter ++;
			System.out.println("thread running...count " + counter);
		    state = true;
			
			
      try {
			t.sleep(timeMillis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
		}
	}
}
